home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / hypercrd / xcmd / dxcmds34.sit / Dartmouth XCMD's 3.4.3 / card_16890.txt < prev    next >
Text File  |  1990-04-17  |  14KB  |  449 lines

  1. -- card: 16890 from stack: in.3
  2. -- bmap block id: 17455
  3. -- flags: 4000
  4. -- background id: 16896
  5. -- name: Replace
  6. ----- HyperTalk script -----
  7. on opencard
  8.   hide card field "source"
  9.   put empty into card field "new"
  10.   pass opencard
  11. end opencard
  12.  
  13. on Install
  14.   put ChooseTargetStack() into it
  15.   InstallResource XFCN,Replace,it
  16. end Install
  17.  
  18.  
  19.  
  20. -- part 2 (field)
  21. -- low flags: 01
  22. -- high flags: 0007
  23. -- rect: left=305 top=58 right=170 bottom=478
  24. -- title width / last selected line: 0
  25. -- icon id / first selected line: 0 / 0
  26. -- text alignment: 0
  27. -- font id: 3
  28. -- text size: 9
  29. -- style flags: 0
  30. -- line height: 12
  31. -- part name: old
  32.  
  33.  
  34. -- part 3 (button)
  35. -- low flags: 00
  36. -- high flags: 8004
  37. -- rect: left=50 top=308 right=324 bottom=225
  38. -- title width / last selected line: 0
  39. -- icon id / first selected line: 0 / 0
  40. -- text alignment: 1
  41. -- font id: 0
  42. -- text size: 12
  43. -- style flags: 0
  44. -- line height: 16
  45. -- part name: Replace Case Sensitive
  46. ----- HyperTalk script -----
  47. on mouseUp
  48.   put empty into card field "new"
  49.   put Replace(card field "old","mytest","myTest",true) into card field "new"
  50.   set the cursor to hand
  51. end mouseUp
  52.  
  53.  
  54.  
  55. -- part 6 (button)
  56. -- low flags: 00
  57. -- high flags: 8003
  58. -- rect: left=304 top=299 right=321 bottom=425
  59. -- title width / last selected line: 0
  60. -- icon id / first selected line: 0 / 0
  61. -- text alignment: 1
  62. -- font id: 0
  63. -- text size: 12
  64. -- style flags: 0
  65. -- line height: 16
  66. -- part name: Show C Source
  67. ----- HyperTalk script -----
  68. on mouseUp
  69.   get the visible of card field "source"
  70.   set the visible of card field "source" to not it
  71.   if it is false then
  72.     set the name of me to "Hide C Source"
  73.   else
  74.     set the name of me to "Show C Source"
  75.   end if
  76. end mouseUp
  77.  
  78.  
  79.  
  80. -- part 8 (field)
  81. -- low flags: 01
  82. -- high flags: 0007
  83. -- rect: left=305 top=181 right=292 bottom=478
  84. -- title width / last selected line: 0
  85. -- icon id / first selected line: 0 / 0
  86. -- text alignment: 0
  87. -- font id: 3
  88. -- text size: 9
  89. -- style flags: 0
  90. -- line height: 12
  91. -- part name: new
  92.  
  93.  
  94. -- part 9 (field)
  95. -- low flags: 01
  96. -- high flags: 2007
  97. -- rect: left=18 top=33 right=288 bottom=290
  98. -- title width / last selected line: 0
  99. -- icon id / first selected line: 0 / 0
  100. -- text alignment: 0
  101. -- font id: 3
  102. -- text size: 10
  103. -- style flags: 0
  104. -- line height: 13
  105. -- part name: Documentation
  106.  
  107.  
  108. -- part 7 (field)
  109. -- low flags: 81
  110. -- high flags: 0007
  111. -- rect: left=18 top=31 right=290 bottom=489
  112. -- title width / last selected line: 0
  113. -- icon id / first selected line: 0 / 0
  114. -- text alignment: 0
  115. -- font id: 3
  116. -- text size: 10
  117. -- style flags: 0
  118. -- line height: 13
  119. -- part name: Source
  120.  
  121.  
  122. -- part 10 (button)
  123. -- low flags: 00
  124. -- high flags: 8004
  125. -- rect: left=50 top=292 right=308 bottom=225
  126. -- title width / last selected line: 0
  127. -- icon id / first selected line: 0 / 0
  128. -- text alignment: 1
  129. -- font id: 0
  130. -- text size: 12
  131. -- style flags: 0
  132. -- line height: 16
  133. -- part name: Replace Case Insensitive
  134. ----- HyperTalk script -----
  135. on mouseUp
  136.   put empty into card field "new"
  137.   put Replace(card field "old","mytest","myTest",false) into card field "new"
  138.   set the cursor to hand
  139. end mouseUp
  140.  
  141.  
  142.  
  143. -- part contents for card part 2
  144. ----- text -----
  145. on test
  146.    global myTest
  147.    put mytest + 1 into MYTest
  148.    if MYTEST > 100 then
  149.       beep
  150.       put 0 into MyTest
  151.   end if
  152. end test
  153.  
  154. -- part contents for card part 7
  155. ----- text -----
  156. /* Replace1.2d1.c */
  157. /* ┬⌐ Trustees of Dartmouth College */
  158. /* written in LightSpeed C  ┬⌐ Think Technologies, Inc */
  159. /* by Roger Brown 7/21/88  Courseware Development Group */
  160.  
  161. /* version 1.2d1 3/30/90 compiles under Think C 4.0 and fixes the bug of replacing
  162.    something with a null string.
  163. /* version 1.2d0 is invisible replace only, using off-screen TextEdit */
  164. /* This uses TextEdit to do the editing, which is not the most efficient
  165.    method, but it is a precursor to a later version that will display
  166.    the text as it is edited and allow user verification. */
  167.    
  168. /* This is a HyperCard XFCN that locates a target string in an input
  169.    container, replaces the string with another string and returns the 
  170.    edited result. Case sensitivity is an optional third parameter.
  171.      
  172.    Syntax is:
  173.    
  174.         get Replace(container,oldString,newString,caseSensitive)
  175.    
  176.                 ex. put Replace(card field 1,"myGlobal","myOtherGlobal",false)
  177.                         into card field 1
  178.   
  179.    returns: a copy of the input container with all occurrences of newString replaced
  180.             by oldString.
  181.    
  182.    where    container   is any hypercard container (field, variable), presumed to be 
  183.                         multi-lined.
  184.             oldString   is the string to look for
  185.             newString   is the string to replace oldString with   
  186.             caseSensitive is an optional parameter turning on case sensitivity   
  187.                                   
  188.       To compile: create a project with this, ANSI-A$ library. and MacTraps. 
  189.         Build as code resource type XFCN named Replace.
  190. */
  191.  
  192.  
  193.  
  194. #include "ToolboxUtil.h"
  195. #include "QuickDraw.h"
  196. #include "TextEdit.h"
  197. #include "HyperXCmd.h"
  198. #include "XCmdGlue.inc.c"
  199. #include "SetUpA4.h"
  200.  
  201. #define FALSE 0
  202. #define TRUE !FALSE
  203. #define MAXINT 32768
  204.  
  205. /* globals */
  206.  
  207. Handle theContainer;                       /* hanle to the original container */
  208. char *oldString;                           /* pointer to the string to replace */
  209. char *newString;                           /* pointer to the string to replace with */
  210. Boolean caseSensitive;                     /* flag - case sensitive match */
  211. Handle resultH;                            /* handle to the edited data */
  212. XCmdBlockPtr   gParamPtr;
  213. TEHandle theTEH;
  214.         
  215.  
  216. /* change a string to all upper case */
  217.  
  218. ucase(s)
  219. char *s;
  220. {
  221. int i;
  222. char c;
  223.         
  224. for (i=0;i<strlen(s);i++) {
  225. s[i] = toupper(s[i]);
  226. }
  227. }
  228.  
  229. /* see if two characters match, maybe ignoring case */
  230.  
  231. matchChar(a,b,theCase)
  232. char a,b;
  233. Boolean theCase;
  234. {
  235.     if (theCase==TRUE) return (a==b);
  236.     return (toupper(a)==toupper(b));
  237. }
  238.  
  239. /* find the target as specified, return start byte */
  240.  
  241. FindIt(theField,len,theTarget,caseSens,start)
  242. char *theField;          /* pointer to field to search */
  243. int len; /* number of bytes to search */
  244. char *theTarget;         /* pointer to target string */
  245. Boolean caseSens;       
  246. int start;                      /*  start is where (byte) to start */
  247. {
  248.         int c;             /* index of current character */
  249.         int tLen;          /* target length */
  250.         int line;          /* line currently being searched */
  251.         int lPos;                  /* character position in line */
  252.         register char *fc,*tc; /* pointers to field and target characters */
  253.          
  254.         tLen = strlen(theTarget);           
  255.         c = lPos = 0; 
  256.         line = 1;                            /* offset into theField */
  257.         fc = theField;                       /* pointer into theField */
  258.         while (c < len) {                   /* scan to end */
  259.             tc = theTarget;                  /* pointer to theTarget */
  260.                 if (*fc==13) {
  261.                          line++;                     /* passing a line boundary */
  262.                          lPos = -1;                  /* reset position */
  263.                 }
  264.                 if ((c) >= start) {             /* don't go until something matches */
  265.                    while (matchChar(*tc,*fc,caseSens)) {   /* check for match */
  266.                             fc++;                      /* cycle through matching characters */
  267.                                 tc++;
  268.                                 if (tc == (theTarget+tLen)){   /* all target characters matched */
  269.                             return c;  /* matched */
  270.                                 }
  271.                         }
  272.                 }
  273.                 c++;                          /* move along */
  274.                 lPos++;
  275.                 fc = theField + c;            /* reset pointer after a search */
  276.         }
  277.         return -1;                         /* target not found */
  278. }
  279.  
  280. /* replace all occurences of the text without showing the user */
  281.  
  282. ReplaceInvisible()
  283. {
  284.         Rect aRect;
  285.         int selStart,selEnd;
  286.         int oPos,nLen,oLen;
  287.         GrafPtr curPort,tempPort;
  288.         
  289.         /* we are going to use TextEdit to do this and TextEdit wants to be in a port
  290.                 so we are going to save the current port, create a new one and move it
  291.                 off screen, do the replacing, and restore the original port. */
  292.                 
  293.         GetPort(&curPort);                     /* save to restore later */
  294.         
  295.         /* open an offscreen graph port */
  296.         tempPort = (GrafPtr)NewPtr(sizeof(GrafPort));
  297.         OpenPort(tempPort);
  298.         MovePortTo(-MAXINT,-MAXINT);
  299.         
  300.         /* create a TextEdit record */
  301.         SetRect(&aRect,-MAXINT,-MAXINT,-MAXINT+200,-MAXINT+200);
  302.         theTEH = TENew(&aRect,&aRect);
  303.         (**theTEH).inPort = tempPort;
  304.  
  305.         oLen = strlen(oldString);
  306.         nLen = strlen(newString);
  307.         
  308.         /* initialize the TERecord with the container text */
  309.         
  310.         TESetText(*theContainer,(long)strlen(*theContainer),theTEH);
  311.         
  312.         /* do the replacing */
  313.         
  314.         oPos = 0;
  315.         selStart = 
  316.         FindIt(*(**theTEH).hText,(**theTEH).teLength,oldString,caseSensitive,oPos);
  317.         while (selStart != -1) {
  318.                 selEnd = selStart + oLen;
  319.                 TESetSelect(selStart,selEnd,theTEH);
  320.                 TECut(theTEH);
  321.                 if (nLen > 0)/* trap  here for null replacement string */
  322.                 TEInsert(newString,(long)nLen,theTEH);
  323.                 oPos = (**theTEH).selEnd;
  324.                 selStart =
  325.                  FindIt(*(**theTEH).hText,(**theTEH).teLength,oldString,caseSensitive,oPos);
  326.         }
  327.         
  328.         /* return the edited text as the result */
  329.         resultH = NewHandle((**theTEH).teLength+1);
  330.         BlockMove((*(**theTEH).hText),*resultH,(**theTEH).teLength);
  331.         *(*resultH+(**theTEH).teLength) = 0;
  332.         
  333.         /* dispose the port and restore the original */
  334.         
  335.         ClosePort(tempPort);
  336.         SetPort(curPort);
  337.         DisposPtr(tempPort);
  338.         
  339. }
  340.  
  341. /* XCMD entry */
  342.  
  343. pascal void Replace(paramPtr)
  344. XCmdBlockPtr    paramPtr;
  345. {   
  346.     int i;
  347.     Str255 paramStr;
  348.     Ptr casePtr,verPtr;
  349.     CursHandle theCursor;
  350.      
  351.     gParamPtr = paramPtr;
  352.     
  353.     /* put up a watch */
  354.     
  355.     theCursor = GetCursor(watchCursor);
  356.     SetCursor(*theCursor);
  357.     
  358.     /* lock parameters down so we can point to themn */
  359.     
  360.     for (i=0;i<paramPtr->paramCount;i++) {
  361.         MoveHHi(paramPtr->params[i]);
  362.         HLock(paramPtr->params[i]);
  363.     }
  364.     
  365.     /* get the parameters */
  366.     
  367.     theContainer = paramPtr->params[0];   /* the container */
  368.     oldString = *(paramPtr->params[1]);  /* the target string */
  369.     newString = *(paramPtr->params[2]);  /* the replace string */
  370.     
  371.     if (paramPtr->paramCount>3)  {       /* case sensitivity flag */
  372.         casePtr = *(paramPtr->params[3]);
  373.         strcpy(paramStr,casePtr);
  374.     }
  375.     else strcpy(paramStr,"");
  376.     ucase(paramStr);
  377.     if (strcmp(paramStr,"TRUE")==0) 
  378.         caseSensitive = TRUE;
  379.     else caseSensitive = FALSE;
  380.   
  381.     ReplaceInvisible();
  382.     
  383.     /* assemble the return string */
  384.     
  385.     paramPtr->returnValue = resultH;
  386.     for (i=0;i<paramPtr->paramCount;i++) HUnlock (paramPtr->params[i]);
  387.     return;
  388. }
  389.  
  390. pascal void main(paramPtr)
  391. XCmdBlockPtr    paramPtr;
  392. /* this is the entry point for the XFCN */
  393. {
  394.         RememberA0();
  395.         SetUpA4();                  /* to access globals */
  396.         Replace(paramPtr);     
  397.         RestoreA4(); 
  398.         return;
  399. }
  400.  
  401.  
  402.  
  403.  
  404. -- part contents for card part 9
  405. ----- text -----
  406. Replace version 1.2d1.c
  407. Roger Brown
  408.  
  409. Replace is an XFCN that replaces all occurrences of specified target text with new text in a HyperCard container.  The input is a container,
  410. the old text, the new text, and a case sensitivity flag. The output is an edited copy of the container data.
  411.  
  412. This version does not support wildcards or pattern matching, and does not allow the user to verify each replacement.  We plan to add these features in a later version, so the source for this utilizes toolbox TextEdit to do the replacement in an off-screen port.  This is not the most efficient method for this particular XFCN, but it serves as a precursor to later development.  The operation is still fairly fast.
  413.  
  414. This XFCN is best used as part of scripts that identify the containers (scripts, fields, globals) to be edited.  For example, the SearchScript utility shipped with HyperCard could be modified to be a ReplaceScript utility by replacing "edit the script of" with this XFCN.
  415.  
  416. INVOKING Replace
  417.  
  418.   get Replace(container,oldText,newText,
  419.           caseSens)
  420.         
  421.    where   container is any HyperCard container
  422.                 oldText is the text to be replaced
  423.                 newText is the text to replace with
  424.                 caseSens is an option flag indicating if
  425.                 the replacement will be case sensitive                                    
  426.  
  427. EXAMPLE
  428.  
  429.  ex. put Replace(the script of this card,"
  430.               myglobal","MyGlobal",false) into the 
  431.               script of this card)
  432.       
  433. This example will correct the upper case appearance of all references to MyGlobal.
  434.  
  435.  
  436. REVISION HISTORY
  437. 1.2d0.c First public release. Non-verify version.
  438. 1.2d1 3/30/90 Fixed hang when replacing any text with a null string.  Compiled with Think C version 4.0.
  439.  
  440. -- part contents for card part 8
  441. ----- text -----
  442. on test
  443.    global myTest
  444.    put myTest + 1 into myTest
  445.    if myTest > 100 then
  446.       beep
  447.       put 0 into myTest
  448.   end if
  449. end test